<template>
  <div class="app-container">
    <el-card class="search-from">
      <el-form :inline="true" class="form-inline" v-model="searchfilter">
        <el-form-item label="角色名称">
          <el-input
            placeholder="请输入角色名称"
            style="width: 300px"
            ref="reffilter"
            v-model="searchfilter.filter"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="search()">查询</el-button>
          <el-button type="primary" :icon="Plus" @click="addRole()">添加</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="list">
      <div class="list-table">
      <el-table style="width: 100%"  :data="dataList?.items">
        <el-table-column
          type="index"
          width="100"
          label="#"
          align="center"
        ></el-table-column>
        <el-table-column
          v-for="item in columns"
          :prop="item.prop"
          :label="item.label"
          :min-width="item.width"
          align="center"
        ></el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="scope">
            <el-button text type="primary" @click="editRoleItem(scope.row)"
              >修改</el-button
            >
            <el-button text type="primary" @click="editRolePermission(scope.row)"
              >权限</el-button
            >
            <el-button text type="primary" @click="deleteRoleItem(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      </div>
      <div class="paging">
        <el-pagination
          background
          layout="prev, pager, next"
          :total="Number(dataList?.totalCount)"
          :page-size="pageSize"
          @change="handlePageChange"
          v-model:current-page="currentPage"
        />
      </div>
    </el-card>
    <CreateRole
      v-if="addRoleFormVisible"
      :FormVisible="addRoleFormVisible"
      @closedialogevent="closedialog"
      @refresh="pg_init"
    />
    <EditRole
      v-if="editFormVisible"
      :FormVisible="editFormVisible"
      :Id="editRoleId"
      @closedialogevent="(eventData:boolean)=>{editFormVisible= eventData}"
      @refresh="pg_init"
    ></EditRole>
    <SetPermission
      v-if="setPermissionFormVisible"
      :title="selectRoleName"
      :Id="editRoleId"
      :rolekey="selectRoleName"
      @closedialogevent="(eventData:boolean)=>{setPermissionFormVisible= eventData}"
    ></SetPermission>
  </div>
</template>

<script setup lang="ts">
import {
  RoleQuery,
  RolePageResult,
  RoleItem,
  RoleDeleteRequest,
} from "@/api/identity/role/types";
import { Search, Plus } from "@element-plus/icons-vue";
import { getRolePage, deleteRole } from "@/api/identity/role/index";
import CreateRole from "./components/CreateRole.vue";
import EditRole from "./components/EditRole.vue";
import SetPermission from "./components/SetPermissionDialog.vue";

const dataList = ref<RolePageResult>();
const currentPage = ref(1);
const pageSize = ref(10);

const addRoleFormVisible = ref<boolean>(false);
const editFormVisible = ref<boolean>(false);
const setPermissionFormVisible = ref<boolean>(false);
const editRoleId = ref<string>();
const selectRoleName = ref<string>("");
let searchfilter = ref<RoleQuery>({
  filter: "",
  skipCount: (currentPage.value - 1) * pageSize.value,
  maxResultCount: pageSize.value,
});

searchfilter.value.filter = "";
const columns = ref([
  {
    label: "角色名称",
    prop: "name",
    width: 300,
  },
]);

function search() {
  pg_init();
}

function pg_init() {
  getRolePage(searchfilter.value).then((res) => {
    dataList.value = res;
  });
}

function handlePageChange(currentPage: number, pageSize: number) {
  searchfilter.value.maxResultCount = pageSize;
  searchfilter.value.skipCount = (currentPage - 1) * pageSize;
  pg_init();
}

function addRole() {
  addRoleFormVisible.value = true;
}

function closedialog(eventData: boolean) {
  addRoleFormVisible.value = eventData;
}
function editRoleItem(row: RoleItem) {
  editFormVisible.value = true;
  editRoleId.value = row.id;
  selectRoleName.value = row.name;
}
function closeEditDialog(eventData: boolean) {
  editFormVisible.value = eventData;
}

function deleteRoleItem(row: RoleItem) {
  ElMessageBox.confirm("确定删除吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      let param: RoleDeleteRequest = { id: row.id };
      deleteRole(param).then((res) => {
        if (res.status === 204) {
          ElNotification({
            title: "提示",
            message: "删除角色成功",
            type: "success",
            duration: 2000,
          });
          pg_init();
        } else {
          ElNotification({
            title: "提示",
            message: "删除角色失败",
            type: "error",
            duration: 2000,
          });
        }
      });
    })
    .catch(() => {
      console.log("取消");
    });
}

function editRolePermission(row: RoleItem) {
  selectRoleName.value = row.name;
  setPermissionFormVisible.value = true;
  editRoleId.value = row.id;
}
pg_init();
</script>

<style scoped>
.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
  font-size: 13px;
}
.dropdown-item {
  font-size: 11px;
}

::v-deep .el-tabs__nav-wrap::after {
  height: 1.5px;
}

.addroleform {
  margin-top: 15px;
  margin-right: 15px;
}

.tabs {
  padding-top: 10px;
  padding-bottom: 20px;
  padding-left: 10px;
  padding-right: 10px;
}
::v-deep .el-divider--horizontal {
  margin: 12px 0;
}

.orgcardscroller {
  height: calc(100vh - 500px);
}

::v-deep .el-scrollbar__wrap {
  overflow-x: hidden;
}
</style>
